package com.rd.student.jiaji.class3.sunguowei.day24;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class TestExecutor1 {
    private static final int CORE_POOL_SIZE = 5;
    private static final int MAX_POOL_SIZE = 20;
    private static final int QUEUE_CAPACITY = 2;
    private static final Long KEEP_ALIVE_TIME = 1L;

    public static void main(String[] args) {
        ThreadPoolExecutor executor=new ThreadPoolExecutor(
                CORE_POOL_SIZE,MAX_POOL_SIZE,
                KEEP_ALIVE_TIME, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(QUEUE_CAPACITY),
                new ThreadPoolExecutor.DiscardPolicy()
        );
        List<Future<String>> futureList=new ArrayList<>();
        //submit() 通过这个方法来提交callable接口的线程
        MyCallable callable=new MyCallable();
        for (int i = 0; i < 12; i++) {
           Future<String> future=executor.submit(callable);
           futureList.add(future);

        }

        for (Future<String> future : futureList) {
//            try {
//                future.get();
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            } catch (ExecutionException e) {
//                throw new RuntimeException(e);
//            }

            //如果可能出现的两个异常  没有父子关系
            //可以将两个异常一同尝试捕捉
            try{
                String result= future.get();
                System.out.println(result);
            }catch (InterruptedException | ExecutionException e){
//                throw new RuntimeException(e);
                e.printStackTrace();
            }
        }
    }
}
